from selenium import webdriver
from urllib.parse import quote, unquote
from jd_crawler.jd_parser.search import search_item
import time

wd_path = r"H:\drivers\chromedriver_86.exe"


class JdSearchSel:
    def __init__(self):
        chrome_option = webdriver.ChromeOptions()
        chrome_option.add_argument("disable-blink-features=AutomationControlled")
        self.browser = webdriver.Chrome(executable_path=wd_path, chrome_options=chrome_option)

    def search(self, keyword):
        self.browser.get("https://www.jd.com/")
        input_ele = self.browser.find_element_by_css_selector("input[aria-label='搜索']")
        input_ele.send_keys(keyword)
        search_button = self.browser.find_element_by_css_selector("button[aria-label='搜索']")
        search_button.click()
        # 页面实际要刷新的时候, 最好休眠几秒
        time.sleep(2)

    def main(self, keyword_array):
        for keyword in keyword_array:
            self.search(keyword)
            item_array = search_item(self.browser.page_source)
            print(item_array)
            # 点击详情页
            # ....
        self.browser.close()


if __name__ == "__main__":
    keyword_array = ["键盘"]
    jd_search = JdSearchSel()
    jd_search.main(keyword_array)

